package com.zycfc.zsf.boot.web.converter;

import org.springframework.core.convert.converter.*;
import java.util.*;
import java.util.regex.*;
import org.apache.commons.lang3.*;
import java.text.*;
import org.slf4j.*;

public class StringToDateConverter implements Converter<String, Date>
{
    private static final Logger log;
    private static final String CN_DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
    private static final String CN_DATE_TIME_FORMAT1 = "yyyy-MM-dd HH:mm";
    private static final String CN_DATE_FORMAT = "yyyy-MM-dd";
    private static final String DATE_TIME_FORMAT = "yyyyMMddHHmmss";
    private static final String DATE_TIME_FORMAT1 = "yyyyMMddHHmm";
    private static final String DATE_FORMAT = "yyyyMMdd";
    private static final String DATE_TIME_FORMAT2 = "yyMMddHHmmss";
    private static final String DATE_FORMAT2 = "yyMMdd";
    private static final String TIME_FORMAT = "HH:mm:ss";
    private static final char CHAR_1 = '-';
    private static final char CHAR_2 = ':';
    private static final Pattern NUM_PATTERN;
    
    public Date convert(String source) {
        StringToDateConverter.log.info("StringToDateConverter=" + source);
        if (StringUtils.isBlank((CharSequence)source)) {
            return null;
        }
        source = source.trim();
        try {
            final int len = source.length();
            final int ind1 = source.indexOf(45);
            final int ind2 = source.indexOf(58);
            SimpleDateFormat formatter = null;
            if (len == "yyyy-MM-dd HH:mm:ss".length() && ind1 != -1 && ind2 != -1) {
                formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            }
            else if (len == "yyyy-MM-dd HH:mm".length() && ind1 != -1 && ind2 != -1) {
                formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            }
            else if (len == "yyyy-MM-dd".length() && ind1 != -1 && ind2 == -1) {
                formatter = new SimpleDateFormat("yyyy-MM-dd");
            }
            else if (len == "yyyyMMddHHmmss".length() && ind1 == -1 && ind2 == -1) {
                formatter = new SimpleDateFormat("yyyyMMddHHmmss");
            }
            else if (len == "yyyyMMddHHmm".length() && ind1 == -1 && ind2 == -1) {
                formatter = new SimpleDateFormat("yyyyMMddHHmm");
            }
            else if (len == "yyyyMMdd".length() && ind1 == -1 && ind2 == -1) {
                formatter = new SimpleDateFormat("yyyyMMdd");
            }
            else if (len == "yyMMddHHmmss".length() && ind1 == -1 && ind2 == -1) {
                formatter = new SimpleDateFormat("yyMMddHHmmss");
            }
            else if (len == "yyMMdd".length() && ind1 == -1 && ind2 == -1) {
                formatter = new SimpleDateFormat("yyMMdd");
            }
            else if (len == "HH:mm:ss".length() && ind1 == -1 && ind2 != -1) {
                formatter = new SimpleDateFormat("HH:mm:ss");
            }
            if (null != formatter) {
                final Date dtDate = formatter.parse(source);
                return dtDate;
            }
            if (ind1 == -1 && ind2 == -1 && StringToDateConverter.NUM_PATTERN.matcher(source).matches()) {
                final Long lDate = new Long(source);
                return new Date(lDate);
            }
        }
        catch (Exception e) {
            StringToDateConverter.log.error(e.getMessage(), (Throwable)e);
            throw new RuntimeException(String.format("parser %s to Date fail", source));
        }
        throw new RuntimeException(String.format("parser %s to Date fail", source));
    }
    
    static {
        log = LoggerFactory.getLogger((Class)StringToDateConverter.class);
        NUM_PATTERN = Pattern.compile("^\\d+$");
    }
}
